//搭建一个express环境
const express = require('express');

const app = express();

const {
  fileOperation
} = require('./utils');

//路由
app.get('/remove', (req, res) => {
  //读取数据
  // let todoList = JSON.parse(readFileSync(resolve(__dirname, 'todo.json'), 'utf8') || '[]');
  /**
   * console.log(todoList);
   * [
   *   { id: 1, content: '运动', completed: true },
   *   { id: 2, content: '读书', completed: true },
   *   { id: 3, content: '吃饭', completed: true }
   * ]
   */

  //删除其中某一项
  // todoList = todoList.filter(todo => todo.id !== 3);

  //AOP: 用户定义的逻辑函数
  function main(todoList) {
    console.log('调试：执行顺序9');
    return todoList = todoList.filter(todo => todo.id !== 3);
  }

  //执行轮子
  const result = fileOperation('todo.json', main);

  //重新写入到todo.json文件里面去
  // writeFileSync(resolve(__dirname, 'todo.json'), JSON.stringify(todoList));

  // console.log(result);

  res.send('ok!');

});

app.listen(8080, function () {
  console.log('listening on 8080');
});